package com.ijiuchuang.jc_localization.model.tools;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.ijiuchuang.jc_localization.bean.bmob.signin.Device;
import com.ijiuchuang.jc_localization.bean.bmob.signin.Student;
import com.ijiuchuang.jc_localization.contract.tools.ToolsSignInContract;
import com.ijiuchuang.jc_localization.tools.utils.Log;
import com.ijiuchuang.jc_localization.tools.utils.MyUtil;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Veev on 2016/7/15
 *
 * @link 384506557@qq.com
 */
public class ToolsSignInModel implements ToolsSignInContract.Model {

    private SharedPreferences shared;
    private Student currentStudent = null;

    @Override
    public Student getCurrentStudent() {
        return currentStudent;
    }

    @Override
    public void setCurrentStudent(Student currentStudent) {
        this.currentStudent = currentStudent;
    }

    @Override
    public void checkStudent(final Context context, Subscriber<Student> subscriber) {
        final String deviceId = MyUtil.getDeviceId(context);
        shared = context.getSharedPreferences("student", Context.MODE_PRIVATE);
        String currentStu = shared.getString("currentStu", "");
        Gson gson = new Gson();
        Student student = gson.fromJson(currentStu, Student.class);

        BmobQuery<Device> query = new BmobQuery<>();
        query.addWhereEqualTo("deviceId", deviceId);
        query.include("student");
        // 查找设备列表
        final Observable<Student> getStudent = query.findObjectsObservable(Device.class)
                // 获取当前设备的device记录
                .concatMap(new Func1<List<Device>, Observable<Device>>() {
                    @Override
                    public Observable<Device> call(List<Device> devices) {
                        // 若不存在记录 新建一条； 若存在，返回第一条
                        if (devices.isEmpty()) {
                            final Device device = new Device();
                            device.setDeviceId(deviceId);
                            device.setPlatform("Android");
                            device.setLocation("九创办公室");
                            return device.saveObservable().map(new Func1<String, Device>() {
                                @Override
                                public Device call(String s) {
                                    device.setObjectId(s);
                                    return device;
                                }
                            });
                        } else {
                            return Observable.just(devices.get(0));
                        }
                    }
                })
                // 过滤 学生不为空的
                .filter(new Func1<Device, Boolean>() {
                    @Override
                    public Boolean call(Device device) {
                        return device.getStudent() != null;
                    }
                })
                .map(new Func1<Device, Student>() {
                    @Override
                    public Student call(Device device) {
                        return device.getStudent();
                    }
                });

        Observable.just(student)
                .concatMap(new Func1<Student, Observable<Student>>() {
                    @Override
                    public Observable<Student> call(Student student) {
                        if (student != null) {
                            return Observable.just(student);
                        } else {
                            return getStudent;
                        }
                    }
                })
                .observeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    @Override
    public void saveStudent(Context context, Student student) {
        shared = context.getSharedPreferences("student", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = shared.edit();
        //用putString的方法保存数据
        Gson gson = new Gson();
        editor.putString("currentStu", gson.toJson(student));
        //提交当前数据
        editor.apply();
    }
}
